package com.yurun.commonapi.aspect;

import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.mapper.UmsPermissionMapper;
import com.yurun.commonapi.pojo.StudentDocument;
import com.yurun.commonapi.pojo.StudentTrainingTable;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.math.BigInteger;
import java.util.Arrays;

@Aspect
@Component
@Order(1)
public class PermissionAop {

    @Autowired
    private UmsPermissionMapper  permissionMapper;

    @Around("@annotation(com.yurun.commonapi.aspect.PermissionAnnotation)")
    public Object permissionCheck(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("===================第一个切面===================：" + System.currentTimeMillis());

        // 获取请求参数，详见接口类
        Object[] arg = joinPoint.getArgs();
        // 获取员工id
        BigInteger  employeeId = (BigInteger) arg[0];
        // 获取员工角色
        String roleName = permissionMapper.getRoleName(employeeId);
        // 如果是机构经理、分店经理、主任、教学负责人等，则直接通过
        if (roleName.equals("机构经理") || roleName.equals("分店经理") || roleName.equals("主任") || roleName.equals("教学负责人")) {
            System.out.println("直接通过");
            return joinPoint.proceed();
        }

        // 获取ifAlone判断是否是训练
        int ifAlone = (int) arg[2];
        System.out.println(ifAlone+"判断");
        if (ifAlone == 0) { // 集体表
            System.out.println("缉拿");
            // 获取该学生训练信息
            StudentTrainingTable studentTrainingTable = (StudentTrainingTable) arg[1];
            if (trainGroup(employeeId, studentTrainingTable, joinPoint)) {
                return joinPoint.proceed();
            }
            return CommonResult.failed("对不起，您没有权限！");
        } else if (ifAlone == 1) { // 个训表
            // 获取该学生训练信息
            StudentTrainingTable studentTrainingTable = (StudentTrainingTable) arg[1];
            if (trainAlone(employeeId, studentTrainingTable, joinPoint)) {
                return joinPoint.proceed();
            }
            return CommonResult.failed("对不起，您没有权限！");
        }
        // 获取该学生档案信息
        StudentDocument studentDocument = (StudentDocument) arg[1];
        // 获取该员工的所有学生
        String studentIds = permissionMapper.getMyStudent(employeeId);
        // 获取该员工的所有班级
        String classroomIds = permissionMapper.getMyClassroom(employeeId);
        System.out.println(employeeId);
        System.out.println(Arrays.toString(studentIds.split(",")) +"u爱说大话");

        // 当studentIds不包含逗号，即学生只有一个时
        if (checkStudentNum(studentDocument.getStudentId(), studentIds)) { // 当该员工的个训学生只有一个时或者有多个时,且学生匹配成功
            return joinPoint.proceed();
        } else if (!classroomIds.contains(",")) { // 当该员工班级只有一个时
            // 获取班级表中的studentIds
            String studentIds_class = permissionMapper.getClassroomStudent(BigInteger.valueOf(Long.parseLong(classroomIds)));
            if (checkStudentNum(studentDocument.getStudentId(), studentIds_class)) { // 当该员工的班级只有一个，且该班级学生只有一个或者有多个时,且学生匹配成功
                return joinPoint.proceed();
            }

        }

        for (String temp : classroomIds.split(",")) { // 该员工的班级有多个
            // 获取班级表中的studentIds
            String studentIds_class = permissionMapper.getClassroomStudent(BigInteger.valueOf(Long.parseLong(temp)));
            if (checkStudentNum(studentDocument.getStudentId(), studentIds_class)) { // 当该员工的班级的学生只有一个时或者有多个时,且学生匹配成功
                return joinPoint.proceed();
            }
            for (String temp1 : studentIds_class.split(",")) { // 当该员工的该班级的学生有多个时
                if (studentDocument.getStudentId().equals(BigInteger.valueOf(Long.parseLong(temp1)))) {
                    return joinPoint.proceed();
                }
            }
        }

        return CommonResult.failed("对不起，您没有权限！");
    }

    public boolean checkStudentNum(BigInteger studentId, String studentIds) {
        if (studentIds != null && !studentIds.contains(",") && studentId.equals(BigInteger.valueOf(Long.parseLong(studentIds)))) {
            return true;
        } else if (studentIds != null && studentIds.contains(",")) {
            for (String temp : studentIds.split(",")) {  // 自己的所有个训学生
                System.out.println("个训"+temp);
                if (studentId.equals(BigInteger.valueOf(Long.parseLong(temp)))) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean trainAlone(BigInteger employeeId, StudentTrainingTable studentTrainingTable, ProceedingJoinPoint joinPoint){
        // 获取该员工的所有学生
        String studentIds = permissionMapper.getMyStudent(employeeId);
        System.out.println(studentIds+"就按送");
        // 获取该员工的所有班级
        String classroomIds = permissionMapper.getMyClassroom(employeeId);
        // 当studentIds不包含逗号，即学生只有一个时
        if (checkStudentNum(studentTrainingTable.getStudentId(), studentIds)) { // 当该员工的个训学生只有一个时或者有多个时,且学生匹配成功
            return true;
        } else if (!classroomIds.contains(",")) { // 当该员工班级只有一个时
            // 获取班级表中的studentIds
            String studentIds_class = permissionMapper.getClassroomStudent(BigInteger.valueOf(Long.parseLong(classroomIds)));
            if (checkStudentNum(studentTrainingTable.getStudentId(), studentIds_class)) { // 当该员工的班级只有一个，且该班级学生只有一个或者有多个时,且学生匹配成功
                return true;
            }

        }

        for (String temp : classroomIds.split(",")) { // 该员工的班级有多个
            // 获取班级表中的studentIds
            String studentIds_class = permissionMapper.getClassroomStudent(BigInteger.valueOf(Long.parseLong(temp)));
            if (checkStudentNum(studentTrainingTable.getStudentId(), studentIds_class)) { // 当该员工的班级的学生只有一个时或者有多个时,且学生匹配成功
                return true;
            }
            for (String temp1 : studentIds_class.split(",")) { // 当该员工的该班级的学生有多个时
                if (studentTrainingTable.getStudentId().equals(BigInteger.valueOf(Long.parseLong(temp1)))) {
                    return true;
                }
            }
        }

        return false;
    }

    public boolean trainGroup(BigInteger employeeId, StudentTrainingTable studentTrainingTable, ProceedingJoinPoint joinPoint){
        // 获取该员工的所有班级
        String classroomIds = permissionMapper.getMyClassroom(employeeId);

        if (!classroomIds.contains(",") && studentTrainingTable.getClassroomId().equals(BigInteger.valueOf(Long.parseLong(classroomIds)))) { // 当该员工班级只有一个且相等时
            return true;
        } else if (classroomIds.contains(",")) {
            for (String temp : classroomIds.split(",")) { // 该员工的班级有多个
                if (studentTrainingTable.getClassroomId().equals(BigInteger.valueOf(Long.parseLong(temp)))) {
                    return true;
                }
            }
        }
        return false;
    }
}
